#!/usr/bin/env python
# coding=utf-8
from logging import Logger
import logging
from logging.handlers import TimedRotatingFileHandler

def init_logger(loggername):
    if loggername not in Logger.manager.loggerDict:
        logger = logging.getLogger(loggername)
        fmt = "[%(asctime)-15s]: %(name)s %(levelname)s %(thread)d %(lineno)d %(message)s"
        datefmt = "%a %d %b %Y %H:%M:%S"
        formatter = logging.Formatter(fmt, datefmt)

        sh = logging.StreamHandler()
        sh.setLevel(logging.DEBUG)
        sh.setFormatter(formatter)

        fh = TimedRotatingFileHandler('output/logging/all.log', when='midnight',backupCount=7)
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(formatter)

        eh = TimedRotatingFileHandler('output/logging/error.log', when='midnight',backupCount=7)
        eh.setLevel(logging.DEBUG)
        eh.setFormatter(formatter)    
        
        logger.addHandler(sh)
        logger.addHandler(fh)
        logger.addHandler(eh)

    logger = logging.getLogger(loggername)
    logger.setLevel(logging.DEBUG)
    return logger

logger = init_logger("PatchDetect")



